﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.ProjectContract;
using Bricks.Business.Models.Account;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.AlterManagement;
using Bricks.Business.Data.Repositories.ProjectContract;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IProjectStageInfoService))]
    internal class ProjectStageInfoService : IProjectStageInfoService
    {
        [Import]
        protected IProjectStageInfoRepository ProjectStageInfoRepository { get; set; }

        public List<ProjectStageInfoView> GetProjectStageInfoList(Guid pid)
        {
            List<ProjectStageInfoView> lstProjectStageInfoView = new List<ProjectStageInfoView>();
            IQueryable<ProjectStageInfo> projectStageInfos = ProjectStageInfoRepository.Entities.Where(c => c.PID == pid).OrderBy(p => p.OrderNo);
            foreach (ProjectStageInfo projectStageInfo in projectStageInfos)
            {
                lstProjectStageInfoView.Add(new ProjectStageInfoView()
                {
                    ID = projectStageInfo.ID,
                    StageName = projectStageInfo.StageName
                });
            }
            return lstProjectStageInfoView;
        }

        public ProjectStageInfoView GetProjectStageInfo(Guid id)
        {
            ProjectStageInfoView projectStageInfoView = null;
            ProjectStageInfo projectStageInfo = ProjectStageInfoRepository.Entities.FirstOrDefault(p => p.ID == id);
            if (projectStageInfo != null)
            {
                projectStageInfoView = new ProjectStageInfoView()
                {
                    ID = projectStageInfo.ID,
                    PID = projectStageInfo.PID,
                    DealPerson = projectStageInfo.DealPerson,
                    StageName = projectStageInfo.StageName
                };
            }
            return projectStageInfoView;
        }

        public OperationResult SaveProjectStageInfo(ProjectStageInfoView projectStageInfoView)
        {

            try
            {
                ProjectStageInfo projectStageInfo = ProjectStageInfoRepository.Entities.FirstOrDefault(p => p.ID == projectStageInfoView.ID);
                if (projectStageInfo == null)
                {
                    Add(projectStageInfoView);
                }
                else
                {
                    Modify(projectStageInfo, projectStageInfoView);
                }
                return new OperationResult(OperationResultType.Success, "操作成功");
            }
            catch (Exception ex)
            {
                return new OperationResult(OperationResultType.Error, ex.Message);
            }
        }

        private void Add(ProjectStageInfoView projectStageInfoView)
        {
            ProjectStageInfo record = new ProjectStageInfo();
            record.ID = projectStageInfoView.ID;
            record.PID = projectStageInfoView.PID;
            record.DealPerson = projectStageInfoView.DealPerson;
            record.StageName = projectStageInfoView.StageName;
            record.OrderNo = ProjectStageInfoRepository.GetNextOrderNo(p => p.OrderNo);
            ProjectStageInfoRepository.Insert(record);
        }

        private void Modify(ProjectStageInfo record, ProjectStageInfoView projectStageInfoView)
        {
            record.StageName = projectStageInfoView.StageName;
            ProjectStageInfoRepository.Update(record);
        }

        public OperationResult Delete(Guid id)
        {
            PublicHelper.CheckArgument(id, "id");
            ProjectStageInfo record = ProjectStageInfoRepository.Entities.FirstOrDefault(s => s.ID == id);
            if (record == null)
                return new OperationResult(OperationResultType.QueryNull, "未找到指定的记录");

            if (ProjectStageInfoRepository.Delete(record) >= 0)
                return new OperationResult(OperationResultType.Success, "删除成功", record);
            return new OperationResult(OperationResultType.Error, "删除失败");
        }

        public OperationResult SetOrder(Guid id, string upOrDown)
        {
            var exeResult = ProjectStageInfoRepository.SetOrder(ProjectStageInfoRepository.Entities, id, "OrderNo", upOrDown);  //执行向上向下操作
            return exeResult;
        }
    }
}